package com.yanqu.road.server.cmd.activity.stronghold.cross;

import com.yanqu.road.entity.activity.stronghold.StrongholdWildReport;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.bussiness.player.activity.UserStrongholdBusiness;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.strongholdwar.StrongholdModule;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import io.netty.channel.ChannelHandlerContext;

@Cmd(code = GameProtocol.S_CROSS_STRONGHOLD_WILD_BEEN_ATTACKED, desc = "野外据点被攻击同步")
public class CrossStrongholdWildBeenAttackedSyncCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        StrongholdWarProto.CrossStrongholdBeenAttackedMsg msg = StrongholdWarProto.CrossStrongholdBeenAttackedMsg.parseFrom(packet.getBytes());
        long userId = msg.getUserId();
        PlayerProto.PlayerBaseTempMsg enemyMsg = msg.getEnemyInfo();
        UserBaseInfo enemyInfo = PlayerBasePb.parseBaseUserInfo(enemyMsg);
        //生成战报
        StrongholdWildReport report = new StrongholdWildReport();
        report.setUserId(userId);
        report.setEnemyServerId(enemyInfo.getServerId());
        report.setEnemyUserBaseInfo(enemyInfo);
        report.setReportTime(System.currentTimeMillis());
        report.setRewardPoints(msg.getRewardPoints());
        report.setBoxList(msg.getBoxList());
        report.setWin(msg.getWin());
        report.setType(msg.getType());
        report.setActivityId(msg.getActivityId());
        report.setEnemyUserId(msg.getEnemyUserId());
        if (msg.getType() == 2) {
            report.setRead(false);
        }else {
            report.setRead(true);
        }
        report.setInsertOption();
        GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
        if (player != null) {
            if (msg.getType() == 2 && msg.getWin()) {
                // 防守 加积分
                int rewardPoints = msg.getRewardPoints();
                player.getModule(StrongholdModule.class).addRewardPoints(rewardPoints);
            }
            //通知
            player.getModule(StrongholdModule.class).receiveWildReport(report);
            StrongholdWarProto.StrongholdBeAttackedSyncMsg.Builder syncMsg = StrongholdWarProto.StrongholdBeAttackedSyncMsg.newBuilder();
            syncMsg.setDispatchPatrons(msg.getDispatchPatrons());
            player.sendPacket(ClientProtocol.U_STRONGHOLD_WAR_SYNC_ATTACKED, syncMsg);
        }else {
            //直接入库吧
            UserStrongholdBusiness.addWildReport(report);
        }
    }
}
